package com.example.student.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.pojo.ClassStudent;
import com.example.common.utils.CacheClient;
import com.example.student.mapper.ClassStudentMapper;
import com.example.student.service.ClassStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ClassStudentServiceImpl extends ServiceImpl<ClassStudentMapper, ClassStudent> implements ClassStudentService {

    @Autowired
    private CacheClient cacheClient;

    @Override
    public List<ClassStudent> getClassStudentByIdWithLogicalExpire(String classId) {
        return cacheClient.queryWithPassThroughList(
                RdsConstants.CLASS_STUDENT_KEY,         // 缓存键前缀
                classId,                                // 班级ID作为参数
                ClassStudent.class,                     // 返回元素类型
                this::getStudentsByClassId,             // 数据库查询方法引用
                RdsConstants.CACHE_Long_TTL,                 // 缓存时间(7天)
                TimeUnit.MINUTES,
                RdsConstants.CACHE_NULL_TTL,                                     // 空值缓存时间
                TimeUnit.MINUTES                        // 空值缓存时间单位
        );
    }


    public List<ClassStudent> getStudentsByClassId(String classId) {
        return this.baseMapper.selectList(new QueryWrapper<ClassStudent>()
                .eq("class_id", classId));
    }


}
